<template>
	<view class="mainpadding">
		<view class="flexbetween pdl" @click="uploadImg()">
			<view class="xiaohei main_size">群头像</view>
			<view class="flexright">
				<image :src="avatar_text" style="width: 80rpx;height: 80rpx;border-radius: 50%;" class="margin_right1" mode=""></image>
				<u-icon name="arrow-right" color="#333" size="12"></u-icon>
			</view>
		</view>
		<view class="flexbetween margin_top pdl">
			<view class="xiaohei main_size">群名称</view>
			<input type="text" class="textright xiaohei main_size" v-model="name" placeholder="请输入群名称" name="" id="">
		</view>
		<view class="margin_top">
			<view class="xiaohei main_size">群描述</view>
			<textarea class="margin_top xiaohei main_size" style="width: 100%;height: 150rpx;" placeholder="请输入群描述"
				v-model="sign" /></textarea>
		</view>
		<view class="submitbtn margin_top" @click="submit">确认创建</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				avatar:"",
				avatar_text:"",
				name:"",
				sign:"",
			}
		},
		methods: {
			submit(){
				if(this.avatar==""){
					httpRequest.toast("请上传头像")
					return false
				}
				if(this.name==""){
					httpRequest.toast("请输入群名称")
					return false
				}
				httpRequest.request('/webim/Groups/addGroup', 'GET', {
					icon:this.avatar,
					name:this.name,
					intro:this.sign,
				}).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = 1;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.avatar = res.url
					this.avatar_text = image_item
				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>
